<?php
	class Thongtinkhuyenmai_model extends CI_Model
	{
		function LayKhuyenMai_CuaMotKhachSan($maks)
		{
			$query = $this->db->get_where('ThongTinKhuyenMai', array('KhachSan' => $maks));
			
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;			
		}
		
		function LayTatCaKhuyenMai_KhachSan_NhanVien()
		{
			$post_per_page = 20;
			$num = 0;
			if($this->uri->segment(3))
				$num = $this->uri->segment(3);			
			
			$strSQL = "SELECT ttkm.*, nv.HoTen, ks.Ten AS TenKS, ncc.TinhTrangTaiKhoan
						FROM ThongTinKhuyenMai ttkm
						JOIN NhanVien nv ON ttkm.NhanVien = nv.MaNV
						JOIN KhachSan ks ON ttkm.KhachSan = ks.MaKS
						JOIN NhaCungCap ncc ON ks.NhaCungCap = ncc.MaNCC
						WHERE ncc.TinhTrangTaiKhoan IS NOT NULL
						ORDER BY ttkm.NgayBatDau DESC, ks.Ten ASC
						LIMIT $num, $post_per_page";
			$query = $this->db->query($strSQL);
			
			$total_query = "SELECT ttkm.*, nv.HoTen, ks.Ten AS TenKS, ncc.TinhTrangTaiKhoan
							FROM ThongTinKhuyenMai ttkm
							JOIN NhanVien nv ON ttkm.NhanVien = nv.MaNV
							JOIN KhachSan ks ON ttkm.KhachSan = ks.MaKS
							JOIN NhaCungCap ncc ON ks.NhaCungCap = ncc.MaNCC
							WHERE ncc.TinhTrangTaiKhoan IS NOT NULL";			
			
			$this->load->library('pagination');
			$config['base_url'] = base_url().'index.php/thongtinkhuyenmai/admin_index';
			$config['total_rows'] = $this->db->query($total_query)->num_rows();			
			$config['per_page'] = $post_per_page;
			$this->pagination->initialize($config);	
						
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;			
		}
		
		function Tim($key)
		{
			$post_per_page = 20;
			$num = 0;
			if($this->uri->segment(4))
				$num = $this->uri->segment(4);
				
			$strSQL = "SELECT ttkm.*, nv.HoTen, ks.Ten AS TenKS, ncc.TinhTrangTaiKhoan
						FROM ThongTinKhuyenMai ttkm
						JOIN NhanVien nv ON ttkm.NhanVien = nv.MaNV
						JOIN KhachSan ks ON ttkm.KhachSan = ks.MaKS
						JOIN NhaCungCap ncc ON ks.NhaCungCap = ncc.MaNCC
						WHERE ncc.TinhTrangTaiKhoan IS NOT NULL
						AND (ttkm.MaKM = '$key' OR ks.Ten LIKE '%$key%')
						ORDER BY ttkm.NgayBatDau DESC, ks.Ten ASC
						LIMIT $num, $post_per_page";
			$query = $this->db->query($strSQL);								
			
			$total_query = "SELECT ttkm.*, nv.HoTen, ks.Ten AS TenKS, ncc.TinhTrangTaiKhoan
							FROM ThongTinKhuyenMai ttkm
							JOIN NhanVien nv ON ttkm.NhanVien = nv.MaNV
							JOIN KhachSan ks ON ttkm.KhachSan = ks.MaKS
							JOIN NhaCungCap ncc ON ks.NhaCungCap = ncc.MaNCC
							WHERE ncc.TinhTrangTaiKhoan IS NOT NULL
							AND (ttkm.MaKM = '$key' OR ks.Ten LIKE '%$key%')";		

			$this->load->library('pagination');
			$config['uri_segment'] = 4;
			$config['base_url'] = base_url().'index.php/thongtinkhuyenmai/Tim/'.$key;
			$config['total_rows'] = $this->db->query($total_query)->num_rows();		
			$config['per_page'] = $post_per_page;
			$this->pagination->initialize($config);	
						
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;			
		}
		
		function LayMaKM_ajax($key)
		{
			$this->db->select('ttkm.MaKM AS Ten');
			$this->db->from('ThongTinKhuyenMai ttkm');
			$this->db->like('ttkm.MaKM', $key);
			$this->db->order_by('MaKM', "asc");
			$query = $this->db->get();
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;			
		}
		
		function LayTenKS_ajax($key)
		{
			$this->db->select('ks.Ten');
			$this->db->from('ThongTinKhuyenMai ttkm');
			$this->db->join('KhachSan ks', 'ttkm.KhachSan = ks.MaKS');			
			$this->db->like('ks.Ten', $key);
			$this->db->order_by('ks.Ten', "asc");
			$this->db->distinct();			
			$query = $this->db->get();
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;			
		}
		
		function LayKhuyenMai_KhachSan_NhanVien($makm)
		{
			$this->db->select('ttkm.*, ks.Ten AS TenKS, nv.HoTen');
			$this->db->from('ThongTinKhuyenMai ttkm');
			$this->db->join('KhachSan ks', 'ttkm.KhachSan = ks.MaKS');
			$this->db->join('NhanVien nv', 'ttkm.NhanVien = nv.MaNV');			
			$this->db->where('ttkm.MaKM', $makm);
			$query = $this->db->get();
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;			
		}
		
		function LayKhuyenMai($makm)
		{
			$query = $this->db->get_where('ThongTinKhuyenMai', array('MaKM' => $makm));
			
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;			
		}
		
		function Xoa($makm)
		{
			return $this->db->delete('ThongTinKhuyenMai', array('MaKM' => $makm));
		}
		
		function XoaNhieuDong($makms)
		{
			if($makms)
			{
				foreach($makms as $makm)
				{
					$data = array('GiaTriKhuyenMai' => 0);
								
					$result_1 = $this->CapNhat($data, $makm);
					
					$this->load->model('phongtrongtheongay_model');
					$result_2 = $this->phongtrongtheongay_model->CapNhat_GiaSauKhuyenMai($makm);					
					
					$result = $this->Xoa($makm);
					if($result < 0 || $result_1 < 0 || $result_2 < 0)
						return $result;
				}
				return 1;
			}
		}
		
		function Them($data)
		{
			$this->db->insert('ThongTinKhuyenMai', $data);
			return $this->db->insert_id();
		}
		
		function CapNhat($data, $makm)
		{
			$this->db->update('ThongTinKhuyenMai', $data, array('MaKM' => $makm)); 
			return $this->db->affected_rows();			
		}			
	}
?>